<?php

namespace app\api\model\user;

use app\logic\model\music\MusicLogicModel;
use app\logic\model\user\MusicCollectionLogicModel;
use app\logic\single\GoodsSingle;

class MusicCollectionApiModel extends MusicCollectionLogicModel
{


    function getApiList($param,$field='*',$page = 1,$limit =1){
        $query=$this
            ->where(function ($q) use ($param){
                if(check_request($param,'type')){
                    $q->where("type",$param['type']);
                }
                if(check_request($param,'user_id')){
                    $q->where("user_id",$param['user_id']);
                }
            });
        $list = $query->field($field)->page($page,$limit)->select();
        $count = $query->count();
        if($count>0){
            $list = GoodsSingle::getInstance()->getGoodsList($list->toArray());
        }
        return [$list,$count];
    }

    public function collection_op($user_id, $goods_id, $type)
    {
        $info = $this->where('user_id',$user_id)
            ->where('goods_id',$goods_id)
            ->where('type',$type)
            ->find();
        if(empty($info)){
            $this->save([
                'user_id'=>$user_id,
                'goods_id'=>$goods_id,
                'type'=>$type,
            ]);
            return 1;
        }else{
            $info->delete();
            return -1;
        }
    }
}